#include "QbTFMessageProcess.h"
#include <qbkernel/include/qb_base.h>
#include <qbprotocol/include/rdProto.h>
#include <qbprotocol/include/SSQBModel.h>
#include <qbprotocol/include/SSQBAModel.h>
#include <qbprotocol/include/MessageParserTool.h>
#include <qbprotocol/include/SSIDBModel.h>
#include <qbprotocol/include/rdMessage.h>
#include <core/Utility.h>
#include <core/strings/string_convert.h>
#include <core/sharedata/login_data.h>
#include <core/time/system_time.h>
#include <qbtools/message/MessagePoster.h>
#include <uam/UserAccountManager.h>
#include <uam/ServerTimeMgr.h>

using namespace qb;

#define Reg_Func(ID) qbBase::getMessageBond()->RegisterDecodeFunc(ID, qbbond::ExtracFunc)
#define DelPtr(FUNC, TYPE) \
	case FUNC:\
	{\
		TYPE* p = static_cast<TYPE*>(toDel);\
		delete p;\
		return nullptr;\
	}\
	break;


#define DECODE_RAWHEAD\
	static char sBuffer[Y_RD_SERVICE_MESSAGE_BLOCK_LEN] = { 0 };\
	int nLenOut = 0;\
	if (!rdMessage::bUnZip((char*)rawvalue.Data, rawvalue.Length, sBuffer, &nLenOut))\
		return nullptr;\
	/*按照字典格式解析数据*/\
	char* sFileBuffer = sBuffer;\
	size_t nStartPos, record_cnt, nFieldCnt;\
	nStartPos = record_cnt = nFieldCnt = 0;\
	std::vector<std::string> vHeaders;\
	/*parse headers*/\
	std::string str;\
	memcpy_s(&nStartPos, sizeof(unsigned int), sFileBuffer + 4, 4);\
	memcpy_s(&record_cnt, sizeof(unsigned int), sFileBuffer + 8, 4);\
	memcpy_s(&nFieldCnt, sizeof(unsigned int), sFileBuffer + 12, 2);\
	sFileBuffer += 14;\
	vHeaders.reserve(nFieldCnt);\
	for (int i = 0; i != nFieldCnt; ++i)\
	{\
		str = sFileBuffer;\
		sFileBuffer = sFileBuffer + str.length() + 2;\
		vHeaders.emplace_back(str);\
	}\
	sFileBuffer = sFileBuffer + 8;\


void qbtfbond::InitDecodeFuncs()
{
//	Reg_Func(E_FID_QB_HIS_SINGLE_DEAL_REG);
}

static void* delAck(int func, void* toDel)
{
	switch (func)
	{
//		DelPtr(E_FID_QB_HIS_SINGLE_DEAL_REG, xSingleBondQuotationAck_c);
	default:
		break;
	}
	return toDel;
}

void* qbtfbond::ExtracFunc(int func, const ProtoMessage& msg, void* toDel)
{
	if (toDel != nullptr)
		return delAck(func, toDel);

	switch (func)
	{
//	case E_FID_QB_BOND_SS_ISSUER_INFO_NEW:
//		return extractBondIssuerInfo(msg);
	default:
		break;
	}

	return nullptr;
}
